package com.arqui.admmon_module.client.core.invalid;

import java.util.List;
import com.gwtplatform.dispatch.shared.DispatchAsync;
import com.gwtplatform.mvp.client.Presenter;
import com.gwtplatform.mvp.client.View;
import com.gwtplatform.mvp.client.annotations.ProxyCodeSplit;
import com.gwtplatform.mvp.client.annotations.NameToken;
import com.arqui.admmon_module.client.place.NameTokens;
import com.gwtplatform.mvp.client.proxy.ProxyPlace;
import com.google.inject.Inject;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.FlexTable;
import com.gwtplatform.mvp.client.proxy.RevealContentEvent;
import com.arqui.admmon_module.client.action.RealTimeInvalid;
import com.arqui.admmon_module.client.action.RealTimeInvalidResult;
import com.arqui.admmon_module.client.core.invalid.CardPresenter;
import com.arqui.admmon_module.shared.InvalidsEntity;

public class RealTimeCardPresenter extends
		Presenter<RealTimeCardPresenter.MyView, RealTimeCardPresenter.MyProxy> {
	private static final int REFRESH_INTERVAL = 2000;

	public interface MyView extends View {
		public FlexTable getRealTimeT();
		public void setRealTimeT(FlexTable realTimeT);
	}

	@ProxyCodeSplit
	@NameToken(NameTokens.realtimecard)
	public interface MyProxy extends ProxyPlace<RealTimeCardPresenter> {
	}

	@Inject
	public RealTimeCardPresenter(final EventBus eventBus, final MyView view,
			final MyProxy proxy) {
		super(eventBus, view, proxy);
	}

	@Override
	protected void revealInParent() {
		RevealContentEvent.fire(this, CardPresenter.SLOT_content, this);
	}
	@Inject DispatchAsync dispatchAsync;

	@Override
	protected void onBind() {
		super.onBind();
		FlexTable realtimeT= getView().getRealTimeT(); 
		realtimeT.setText(0, 0, "ID del archivo");
		realtimeT.setText(0, 1, "No. de linea");
		realtimeT.setText(0, 2, "Almacen");
		realtimeT.setText(0, 3, "Sucursal");
		realtimeT.setText(0, 4, "Fecha y hora");
		realtimeT.setText(0, 5, "Franquicia");
		realtimeT.setSize("957px", "27px");
		Timer refreshTimer = new Timer() {
			RealTimeInvalid action = new RealTimeInvalid("2");
			public void run()
			{
				refreshTable();
			}

			private void refreshTable() {
				dispatchAsync.execute(action, getRealTimeInvalid);
			}
		};
		refreshTimer.scheduleRepeating(REFRESH_INTERVAL);
	
	}
	
	private final AsyncCallback<RealTimeInvalidResult> getRealTimeInvalid = new AsyncCallback<RealTimeInvalidResult>() {
		
		@Override
		public void onSuccess(RealTimeInvalidResult result) {
			List<InvalidsEntity> events =result.getResult();
			int n= getView().getRealTimeT().getRowCount();
			for(int i=n;i<n+events.size();n++){
				getView().getRealTimeT().setHTML(i, 0, events.get(i).getId());
				getView().getRealTimeT().setHTML(i, 1, events.get(i).getLine());
				getView().getRealTimeT().setHTML(i, 2, events.get(i).getStore());
				getView().getRealTimeT().setHTML(i, 3, events.get(i).getBranch());
				getView().getRealTimeT().setHTML(i, 4, events.get(i).getDate());
				getView().getRealTimeT().setHTML(i, 5, events.get(i).getFranchise());
			}
		}
		
		@Override
		public void onFailure(Throwable caught) {		
		}
	};
}